﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsTemplate
    {
        private readonly KeRui.DAL.CmsTemplate dal = new KeRui.DAL.CmsTemplate();

        public int Add(KeRui.Model.CmsTemplate model)
        {
            return this.dal.Add(model);
        }

        private List<KeRui.Model.CmsTemplate> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsTemplate> list = new List<KeRui.Model.CmsTemplate>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsTemplate item = new KeRui.Model.CmsTemplate();
                    if (dt.Rows[i]["TemplateID"].ToString() != "")
                    {
                        item.TemplateID = int.Parse(dt.Rows[i]["TemplateID"].ToString());
                    }
                    item.TemplateName = dt.Rows[i]["TemplateName"].ToString();
                    item.TemplateFolder = dt.Rows[i]["TemplateFolder"].ToString();
                    item.TemplateDir = dt.Rows[i]["TemplateDir"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        item.IsUsing = int.Parse(dt.Rows[i]["IsUsing"].ToString());
                    }
                    if (dt.Rows[i]["SiteNum"].ToString() != "")
                    {
                        item.SiteNum = int.Parse(dt.Rows[i]["SiteNum"].ToString());
                    }
                    if (dt.Rows[i]["ServerID"].ToString() != "")
                    {
                        item.ServerID = int.Parse(dt.Rows[i]["ServerID"].ToString());
                    }
                    item.ServerName = dt.Rows[i]["ServerName"].ToString();
                    item.ServerIP = dt.Rows[i]["ServerIP"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int TemplateID)
        {
            return this.dal.Delete(TemplateID);
        }

        public List<KeRui.Model.CmsTemplate> GetList(int ServerID)
        {
            string strWhere = "";
            if (ServerID > 0)
            {
                strWhere = "ServerID=" + ServerID;
            }
            string strOrder = "templateid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsTemplate", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsTemplate> GetList(string strWhere)
        {
            string strOrder = "templateid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsTemplate", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsTemplate> GetListByCache(int ServerID)
        {
            string cacheKey = "CmsTemplateList-" + ServerID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(ServerID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsTemplate>) cache;
        }

        public KeRui.Model.CmsTemplate GetModel(int TemplateID)
        {
            return this.dal.GetModel(TemplateID);
        }

        public KeRui.Model.CmsTemplate GetModel(int ServerID, string TemplateFolder)
        {
            return this.dal.GetModel(ServerID, TemplateFolder);
        }

        public KeRui.Model.CmsTemplate GetModelByCache(int TemplateID)
        {
            string cacheKey = "CmsTemplateModel-" + TemplateID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(TemplateID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsTemplate) cache;
        }

        public DataSet GetSelect(int ServerID)
        {
            string strWhere = "";
            strWhere = "ServerID=" + ServerID;
            string strOrder = "templateid asc";
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsTemplate", strWhere, strOrder, " TemplateID,TemplateFolder");
        }

        public int Update(KeRui.Model.CmsTemplate model)
        {
            return this.dal.Update(model);
        }
    }
}

